package com.acme.gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JPanel;

public class MenuBar extends javax.swing.JPanel {

    private JPanel cardPanel;

    public MenuBar() {
        initComponents();
    }
    private static int HEADER_HEIGHT = 25;
    private static Color BACKGROUND_COLOR_1 = new Color(0x9e9e9e);
    private static Color BACKGROUND_COLOR_2 = new Color(0x9c9c9c);
    private static Color BACKGROUND_COLOR_3 = new Color(0x999999);
    private static Color BACKGROUND_COLOR_4 = new Color(0x979797);
    private static Color BORDER_COLOR = new Color(0x313131);
    private static Color TOP_SHADOW_COLOR_1 = new Color(0x9f9f9f);
    private static Color TOP_SHADOW_COLOR_2 = new Color(0x9f9f9f);
    private static Color TOP_SHADOW_COLOR_3 = new Color(0x9e9e9e);
    private static Color BOTTOM_SHADOW_COLOR_1 = new Color(0x979797);
    private static Color BOTTOM_SHADOW_COLOR_2 = new Color(0x969696);

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(-1, HEADER_HEIGHT);
    }

    private void configureButtonUI(Component comp) {
        if (comp instanceof AbstractButton) {
            AbstractButton button = (AbstractButton) comp;
            button.setCursor(new Cursor(Cursor.HAND_CURSOR));
            button.addActionListener(new ButtonActionListener());
            button.setUI(ui);
        }
    }

    public JPanel getCardPanel() {
        return cardPanel;
    }

    public void setCardPanel(JPanel cardPanel) {
        if( cardPanel.getLayout() instanceof CardLayout ) {
            this.cardPanel = cardPanel;
        }
    }

    // <editor-fold defaultstate="collapsed" desc="private JPanel createButtonPanel(){...}">
    private JPanel createButtonPanel() {
        return new JPanel() {

            @Override
            public Component add(Component comp) {
                configureButtonUI(comp);
                return super.add(comp);
            }

            @Override
            public Component add(Component comp, int index) {
                configureButtonUI(comp);
                return super.add(comp, index);
            }

            @Override
            public void add(Component comp, Object constraints) {
                configureButtonUI(comp);
                super.add(comp, constraints);
            }

            @Override
            public void add(Component comp, Object constraints, int index) {
                configureButtonUI(comp);
                super.add(comp, constraints, index);
            }
        };
    }// </editor-fold>
    private class ButtonActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            AbstractButton button = (AbstractButton) e.getSource();
            if(cardPanel != null) {
                CardLayout layout = (CardLayout) cardPanel.getLayout();
                layout.show(cardPanel, button.getText());
            }
        }
    }

    

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        ui = new com.acme.gui.util.ITunesHeaderButtonUI();
        buttonPanel = createButtonPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jToggleButton11 = new javax.swing.JToggleButton();
        jToggleButton3 = new javax.swing.JToggleButton();
        jToggleButton4 = new javax.swing.JToggleButton();
        jToggleButton5 = new javax.swing.JToggleButton();
        jToggleButton6 = new javax.swing.JToggleButton();
        jToggleButton7 = new javax.swing.JToggleButton();
        jToggleButton8 = new javax.swing.JToggleButton();
        jToggleButton9 = new javax.swing.JToggleButton();
        jToggleButton10 = new javax.swing.JToggleButton();

        setMaximumSize(new java.awt.Dimension(2147483647, 19));
        setMinimumSize(new java.awt.Dimension(370, 19));
        setLayout(new java.awt.BorderLayout());

        buttonPanel.setMaximumSize(new java.awt.Dimension(32767, 22));
        buttonPanel.setMinimumSize(new java.awt.Dimension(370, 22));
        buttonPanel.setOpaque(false);
        buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));

        buttonGroup1.add(jToggleButton1);
        jToggleButton1.setFont(new java.awt.Font("Lucida Grande", 1, 10));
        jToggleButton1.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton1.setSelected(true);
        jToggleButton1.setText("CARRETERAS");
        jToggleButton1.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton1.setBorderPainted(false);
        jToggleButton1.setContentAreaFilled(false);
        jToggleButton1.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton1);

        buttonGroup1.add(jToggleButton2);
        jToggleButton2.setFont(new java.awt.Font("Lucida Grande", 1, 10));
        jToggleButton2.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton2.setText("TRAMOS");
        jToggleButton2.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton2.setBorderPainted(false);
        jToggleButton2.setContentAreaFilled(false);
        jToggleButton2.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton2);

        buttonGroup1.add(jToggleButton11);
        jToggleButton11.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N
        jToggleButton11.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton11.setText("UBICACIONES");
        jToggleButton11.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton11.setBorderPainted(false);
        jToggleButton11.setContentAreaFilled(false);
        jToggleButton11.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton11);

        buttonGroup1.add(jToggleButton3);
        jToggleButton3.setFont(new java.awt.Font("Lucida Grande", 1, 10));
        jToggleButton3.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton3.setText("EMPLEADOS");
        jToggleButton3.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton3.setBorderPainted(false);
        jToggleButton3.setContentAreaFilled(false);
        jToggleButton3.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton3);

        buttonGroup1.add(jToggleButton4);
        jToggleButton4.setFont(new java.awt.Font("Lucida Grande", 1, 10));
        jToggleButton4.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton4.setText("CASETAS");
        jToggleButton4.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton4.setBorderPainted(false);
        jToggleButton4.setContentAreaFilled(false);
        jToggleButton4.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton4);

        buttonGroup1.add(jToggleButton5);
        jToggleButton5.setFont(new java.awt.Font("Lucida Grande", 1, 10));
        jToggleButton5.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton5.setText("POBLADOS");
        jToggleButton5.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton5.setBorderPainted(false);
        jToggleButton5.setContentAreaFilled(false);
        jToggleButton5.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton5);

        buttonGroup1.add(jToggleButton6);
        jToggleButton6.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N
        jToggleButton6.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton6.setText("Z. RIESGO");
        jToggleButton6.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton6.setBorderPainted(false);
        jToggleButton6.setContentAreaFilled(false);
        jToggleButton6.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton6);

        buttonGroup1.add(jToggleButton7);
        jToggleButton7.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N
        jToggleButton7.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton7.setText("ENTRONQUES");
        jToggleButton7.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton7.setBorderPainted(false);
        jToggleButton7.setContentAreaFilled(false);
        jToggleButton7.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton7);

        buttonGroup1.add(jToggleButton8);
        jToggleButton8.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N
        jToggleButton8.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton8.setText("GASOLINERAS");
        jToggleButton8.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton8.setBorderPainted(false);
        jToggleButton8.setContentAreaFilled(false);
        jToggleButton8.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton8);

        buttonGroup1.add(jToggleButton9);
        jToggleButton9.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N
        jToggleButton9.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton9.setText("RETORNOS");
        jToggleButton9.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton9.setBorderPainted(false);
        jToggleButton9.setContentAreaFilled(false);
        jToggleButton9.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton9);

        buttonGroup1.add(jToggleButton10);
        jToggleButton10.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N
        jToggleButton10.setForeground(new java.awt.Color(255, 255, 255));
        jToggleButton10.setText("SERVICIOS");
        jToggleButton10.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 4, 2, 4));
        jToggleButton10.setBorderPainted(false);
        jToggleButton10.setContentAreaFilled(false);
        jToggleButton10.setPreferredSize(new java.awt.Dimension(80, 24));
        buttonPanel.add(jToggleButton10);

        add(buttonPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D graphics = (Graphics2D) g.create();
        int midY = getHeight() / 2;
        Paint topPaint = new GradientPaint(0, 0, BACKGROUND_COLOR_1,
                0, midY, BACKGROUND_COLOR_2);
        graphics.setPaint(topPaint);
        graphics.fillRect(0, 0, getWidth(), midY);
        Paint bottomPaint = new GradientPaint(0, midY + 1, BACKGROUND_COLOR_3,
                0, getHeight(), BACKGROUND_COLOR_4);
        graphics.setPaint(bottomPaint);
        graphics.fillRect(0, midY, getWidth(), getHeight());
        graphics.setColor(TOP_SHADOW_COLOR_1);
        graphics.drawLine(0, 1, getWidth(), 1);
        graphics.setColor(TOP_SHADOW_COLOR_2);
        graphics.drawLine(0, 2, getWidth(), 2);
        graphics.setColor(TOP_SHADOW_COLOR_3);
        graphics.drawLine(0, 3, getWidth(), 3);
        graphics.setColor(BOTTOM_SHADOW_COLOR_1);
        graphics.drawLine(0, getHeight() - 3, getWidth(), getHeight() - 3);
        graphics.setColor(BOTTOM_SHADOW_COLOR_2);
        graphics.drawLine(0, getHeight() - 2, getWidth(), getHeight() - 2);
        graphics.setColor(BORDER_COLOR);
        graphics.drawLine(0, 0, getWidth(), 0);
        graphics.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
        graphics.dispose();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton10;
    private javax.swing.JToggleButton jToggleButton11;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToggleButton jToggleButton4;
    private javax.swing.JToggleButton jToggleButton5;
    private javax.swing.JToggleButton jToggleButton6;
    private javax.swing.JToggleButton jToggleButton7;
    private javax.swing.JToggleButton jToggleButton8;
    private javax.swing.JToggleButton jToggleButton9;
    private com.acme.gui.util.ITunesHeaderButtonUI ui;
    // End of variables declaration//GEN-END:variables
}
